import { createRouter, createWebHistory } from 'vue-router';
import HomeView from '../views/HomeView.vue';

export const routes = [
	{
		path: '/',
		redirect: '/login',
	},
	{
		path: '/login',
		component: () => import('../views/Login.vue'),
	},
	{
		path: '/home',
		name: 'home',
		meta: {
			name: '首页',
		},
		component: HomeView,
		children: [
			{
				path: 'manage',
				name: 'manage',
				label: '账户管理',
				meta: {
					name: '账户管理',
				},
				component: () => import('../views/user/UserView.vue'),
				children: [
					{
						path: 'managelist',
						name: 'managelist',
						label: '管理员列表',
						meta: {
							name: '管理员列表',
						},
						component: () =>
							import('../views/user/UserList.vue'),
					},
				],
			},
			{
				path: 'swipemanage',
				name: 'swipemanage',
				label: '轮播图管理',
				meta: {
					name: '轮播图管理',
				},
				component: () => import('../views/swipe/SwipeView.vue'),
				children: [
					{
						path: 'swipelist',
						name: 'swipelist',
						label: '轮播图列表',
						meta: {
							name: '轮播图列表',
						},
						component: () =>
							import('../views/swipe/SwipeList.vue'),
					},
					{
						path: 'swipeadd',
						name: 'swipeadd',
						label: '添加轮播图',
						meta: {
							name: '添加轮播图',
						},
						component: () =>
							import('../views/swipe/AddSwipe.vue'),
					},
				],
			},
			{
				path: 'productmanage',
				name: 'productmanage',
				label: '产品管理',
				meta: {
					name: '产品管理',
				},
				component: () => import('../views/produce/ProduceView.vue'),
				children: [
					{
						path: 'productlist',
						name: 'productlist',
						label: '产品列表',
						meta: {
							name: '产品列表',
						},
						component: () =>
							import('../views/produce/ProduceList.vue'),
					},
					{
						path: 'secondkilllist',
						name: 'secondkilllist',
						label: '秒杀列表',
						meta: {
							name: '秒杀列表',
						},
						component: () =>
							import('../views/produce/KillList.vue'),
					},
					{
						path: 'recomendlist',
						name: 'recomendlist',
						label: '推荐列表',
						meta: {
							name: '推荐列表',
						},
						component: () =>
							import('../views/produce/RecomendList.vue'),
					},
					{
						path: 'filterlist',
						name: 'filterlist',
						label: '筛选列表',
						meta: {
							name: '筛选列表',
						},
						component: () =>
							import('../views/produce/FilterList.vue'),
					},
				],
			},
			{
				path: 'echartsmanage',
				name: 'echartsmanage',
				label: '数据可视化',
				meta: {
					name: '数据可视化',
				},
				component: () => import('../views/echarts/EchartsView.vue'),
				children: [
					{
						path: 'echartsshow',
						name: 'echartsshow',
						label: 'Echarts数据展示',
						meta: {
							name: 'Echarts数据展示',
						},
						component: () =>
							import('../views/echarts/EchartsList.vue'),
					},
					{
						path: 'echartsother',
						name: 'echartsother',
						label: 'Echarts其他数据',
						meta: {
							name: 'Echarts其他数据',
						},
						component: () =>
							import('../views/echarts/EchartsData.vue'),
					},
				],
			},
			{
				path: 'editor',
				name: 'editor',
				label: '编辑器',
				meta: {
					name: '编辑器',
				},
				component: () => import('../views/editor/EditorView.vue'),
				children: [
					{
						path: 'editorsubmit',
						name: 'editorsubmit',
						label: '富文本编辑器',
						meta: {
							name: '富文本编辑器',
						},
						component: () =>
							import('../views/editor/EditorList.vue'),
					},
				],
			},
			{
				path: 'map',
				name: 'map',
				label: '地图',
				meta: {
					name: '地图',
				},
				component: () => import('../views/map/MapView.vue'),
				children: [
					{
						path: 'mapbaidu',
						name: 'mapbaidu',
						label: '我的地图',
						meta: {
							name: '我的地图',
						},
						component: () => import('../views/map/MapList.vue'),
					},
				],
			},
		],
	},
	{
		path: '/about',
		name: 'about',
		// route level code-splitting
		// this generates a separate chunk (About.[hash].js) for this route
		// which is lazy-loaded when the route is visited.
		component: () => import('../views/AboutView.vue'),
	},
];

const router = createRouter({
	history: createWebHistory(import.meta.env.BASE_URL),
	routes,
});

router.beforeEach((to, from) => {
	if (to.fullPath === '/login') {
		//已经登录了，就直接跳转到home页面
		if (localStorage.getItem('token')) {
			return { path: '/home' };
		}
		return true;
	} else if (localStorage.getItem('token')) {
		//已经登录了，直接跳转
		return true;
	} else {
		return { path: '/login' }; //如果没有登录就跳转到登录页面
	}
});
export default router;
